package servlet;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class SellRequestProcess extends HttpServlet {
    
    private ServletContext sc;
    private String savePath;
    
    private HashMap<String,String> sellMap = new HashMap<String,String>();
    

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        request.setCharacterEncoding("UTF-8");
    
        System.out.println("-1");
        
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload servletFileUpload = new ServletFileUpload(factory);//通过工厂生成一个处理文件上传的servlet对象
        
        System.out.println("-2");
        
        try {
        	 System.out.println("-3");
        	
            List<FileItem> items = servletFileUpload.parseRequest(request);//解析request
            
            System.out.println("-3.1");
            
            Iterator<FileItem> iterator = items.iterator();
            
            System.out.println("-4");
            
            while (iterator.hasNext()) {
                FileItem item = (FileItem) iterator.next();
            
                System.out.println("-5");
                
                if(item.isFormField()){//表单的参数字段
                	
                	sellMap.put(item.getFieldName(),item.getString("UTF-8"));
                	System.out.println(sellMap);
                    System.out.println("form para name: "+item.getFieldName()+",form para vale: "+item.getString("UTF-8"));
                    // form para name: itemname,form para vale: hory potel
                    System.out.println("-6");
                    
                }else {
                    if(item.getName()!=null && !item.getName().equals("")){//一个上传的文件
                        System.out.println("file name: "+item.getName());
                        System.out.println("file size: "+item.getSize());
                        System.out.println("file type: "+item.getContentType());
                        
                        System.out.println("-7");
                        
                        File tempFile = new File(item.getName());//getName得到的文件名称包含了它在客户端的路径
                        
                        System.out.println("-7.1: " + tempFile.getAbsolutePath());
                        System.out.println("-7.2: " + tempFile.getName());
                        
                        System.out.println("-8");
                        System.out.println("-8.1: " + sc.getRealPath("/"));
                        System.out.println("-8.11: " + sc);
                        System.out.println("-8.2: " + savePath);
                        
                        File file = new File(sc.getRealPath("/")+savePath,tempFile.getName());
                        
                        System.out.println(file.getAbsolutePath());
                        
                        System.out.println("-9");
                        
                        item.write(file);//将上传的文件写入到file中
                        
                        System.out.println("-10");
                        
                        request.setAttribute("message", "Picture Upload Sucess");
                    }else{
                        request.setAttribute("message", "No Picture Uploaded");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "Picture Upload Failed");
        }
        
        request.getRequestDispatcher("/sellUploadResult.jsp").forward(request, response);//转向，输出结果
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
//       savePath = config.getInitParameter("savePath");
        savePath = "img";
        sc = config.getServletContext();        
    }
    
}

/*
-1
-2
-3
-3.1
-4
-5
form para name: itemname,form para vale: hory potel
-6
-5
form para name: category,form para vale: book
-6
-5
form para name: itemdetail,form para vale: boy
-6
-5
file name: baby.jpg
file size: 16394
file type: image/jpeg
-7
-7.1: D:\Tools\development\apache-tomcat-7.0.41-windows-x86\apache-tomcat-7.0.41\bin\baby.jpg
-7.2: baby.jpg
-8
-8.1: D:\Tools\development\apache-tomcat-7.0.41-windows-x86\apache-tomcat-7.0.41\webapps\nidaye\
-8.11: org.apache.catalina.core.ApplicationContextFacade@2d14d18f
-8.2: img
D:\Tools\development\apache-tomcat-7.0.41-windows-x86\apache-tomcat-7.0.41\webapps\nidaye\img\baby.jpg
-9
-10
-5
form para name: duration,form para vale: 10day
-6
-5
form para name: uid,form para vale: 123
-6
-5
form para name: type,form para vale: sell
-6
 */
